package cn.kgc.controller;


import cn.kgc.dto.Result;
import cn.kgc.pojo.Model;
import cn.kgc.service.ModelService;
import cn.kgc.utils.redis.ResultUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 商品型号 前端控制器
 * </p>
 *
 * @author qf
 * @since 2022-03-03
 */
@RestController
@RequestMapping("/api/model")
public class ModelController {
        private final ModelService modelService;

    public ModelController(ModelService modelService) {
        this.modelService = modelService;
    }

    @GetMapping("/search")
    public Result findAll(Model model,Integer pageNo,Integer pageSize){
        if(pageSize==null) {
            pageSize = 2;
        }
            Page<Model> page=new Page<>(pageNo,pageSize);
            QueryWrapper<Model> wrapper = new QueryWrapper<>();
            if(model.getClassifyId()!=null&& model.getClassifyId()!=0){
                wrapper.eq("classifyId",model.getClassifyId());
            }
            if(model.getBrandId()!=null&& model.getBrandId()!=0){
                wrapper.eq("brandId",model.getBrandId());
            }
            if(model.getModelName()!=null&&!"".equals(model.getModelName())){
                wrapper.like("ModelName",model.getModelName());
            }

           page = modelService.page(page,wrapper);
            Result result = ResultUtils.returnDataSuccess(page);
            return result;
        }
        @GetMapping("/{id}/detail")
    public Result search(@PathVariable("id") Long id){
            Model byId = modelService.getById(id);
            return ResultUtils.returnDataSuccess(byId);
        }
}

